<template>
  <div class="employees">
    <!-- 工具栏组件 -->
    <mytools>
      <template #right>
        <el-button size="mini" type="primary" @click="toimport">导入</el-button>
        <el-button @click="openAdd" size="mini" type="primary">
          <i class="el-icon-plus"></i>新增员工
        </el-button>
      </template>
    </mytools>
    <!-- 表格数据 -->
    <el-card class="mycard">
      <el-table :data="list">
        <el-table-column
          label="序号"
          type="index"
          fixed="left"
        ></el-table-column>
        <el-table-column label="姓名" prop="username"></el-table-column>
        <el-table-column label="头像">
          <template slot-scope="{ row }">
            <img
              @click="openewm(row.staffPhoto)"
              class="avatar"
              :src="row.staffPhoto"
              alt=""
              v-errorimg="defaultimg"
            />
          </template>
        </el-table-column>
        <el-table-column label="手机号" prop="mobile"></el-table-column>
        <el-table-column label="工号" prop="workNumber"></el-table-column>
        <el-table-column label="聘用形势" prop="formOfEmployment">
          <template slot-scope="{ row }">
            {{ row.formOfEmployment | myEmpolyees }}
          </template>
        </el-table-column>
        <el-table-column label="部门" prop="departmentName"></el-table-column>
        <el-table-column label="入职时间">
          <template slot-scope="{ row }">
            {{ row.timeOfEntry | timeFormata }}
          </template>
        </el-table-column>
        <el-table-column label="是否在职">
          <template>在职</template>
        </el-table-column>
        <el-table-column label="状态">
          <template>可用</template>
        </el-table-column>
        <el-table-column label="操作" width="300" fixed="right">
          <template slot-scope="{ row }">
            <el-button
              :disabled="'employees-check' | hasRight"
              @click="toDetail(row)"
              type="text"
              >查看</el-button
            >
            <el-button :disabled="'employees-trun' | hasRight" type="text"
              >转正</el-button
            >
            <el-button type="text">调岗</el-button>
            <el-button type="text">离职</el-button>
            <el-button @click="openRole(row)" type="text">角色</el-button>
            <el-button type="text">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        class="pagination"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="pageSizes"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <prccesspanel
      @updateData="getEmployeesAllList"
      :dialogShow.sync="dialogShow"
    ></prccesspanel>
    <!-- 头像转二维码之后的面板 -->
    <el-dialog title="查看图片的二维码" :visible.sync="ewmShow">
      <!-- 显示二维码 -->
      <canvas ref="myCanvas" />
    </el-dialog>
    <!-- 分配角色 -->
    <el-dialog title="分配角色" :visible.sync="roleShow">
      <el-form>
        <el-form-item>
          {{ checklist }}
          <!-- 复选框组 -->
          <el-checkbox-group v-model="checklist">
            <el-checkbox
              v-for="(item, index) in roleList"
              :key="index"
              :label="item.id"
            >
              {{ item.name }}
            </el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item class="myitem">
          <el-button @click="roleShow = false">取 消</el-button>
          <el-button @click="submit" type="primary">确 定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  getEmployeesAllListApi,
  getEmployeesApi,
  setEmployeesRoleApi
} from '@/api/employees'
import { getRoleListApi } from '@/api/setting'
import QrCode from 'qrcode'
export default {
  data () {
    return {
      // 员工列表数据源
      list: [{ name: 'xjj' }],
      // 当前页
      currentPage: 1,
      // 页容量的集合
      pageSizes: [8, 16, 24, 32],
      // 当前页容量
      pageSize: 8,
      // 总条数
      total: 0,
      // 控制面板的显示与隐藏
      dialogShow: false,
      defaultimg: require('@/assets/common/img.jpeg'),
      // 控制二维码面板的显示与隐藏
      ewmShow: false,
      // 控制角色面板的显示与隐藏
      roleShow: false,
      // 复选框中选中的内容
      checklist: [],
      // 角色列表
      roleList: [],
      // 当前操作的员工
      currentObj: {}
    }
  },
  methods: {
    // 修改用户的角色
    async submit () {
      try {
        // 提示修改后的角色数据
        await setEmployeesRoleApi({
          id: this.currentObj.id,
          roleIds: this.checklist
        })
        // 提示修改成功
        this.$message.success('角色分配成功')
        // 关闭面板
        this.roleShow = false
        // 更新数据
        this.getEmployeesAllList()
      } catch (error) {}
    },
    // 打开角色面板
    async openRole (row) {
      // 保存当前操作的对象
      this.currentObj = row
      this.roleShow = true
      // 根据 id 得到当前员工的角色
      const res = await getEmployeesApi(row.id)
      // console.log(res)
      // 将当前员工的角色信息保存起来：
      this.checklist = res.data.roleIds
    },
    // 得到所有的角色列表
    async getRoleList () {
      const res = await getRoleListApi()
      // 保存角色列表
      this.roleList = res.data.rows
    },
    // 打开二维码面板
    openewm (img) {
      this.ewmShow = true
      this.$nextTick(() => {
        // 将 img 地址转为二维码
        QrCode.toCanvas(this.$refs.myCanvas, img, {
          width: 200,
          height: 200
        })
      })
    },
    // 跳转到详情页面
    toDetail (row) {
      this.$router.push(`/employees/details/${row.id}`)
    },
    // 跳转到批量导入的页面
    toimport () {
      this.$router.push('/employees/import')
    },
    // 打开新增页面
    openAdd () {
      this.dialogShow = true
    },
    // 获取所有的员工列表
    async getEmployeesAllList () {
      const res = await getEmployeesAllListApi({
        page: this.currentPage,
        size: this.pageSize
      })
      // 保存数据
      this.list = res.data.rows
      this.total = res.data.total
    },
    // 页容量改变时触发
    handleSizeChange (size) {
      this.pageSize = size
      this.getEmployeesAllList()
    },
    // 当前页改变时触发
    handleCurrentChange (page) {
      this.currentPage = page
      this.getEmployeesAllList()
    }
  },
  created () {
    // 得到员工列表
    this.getEmployeesAllList()
    // 得到角色列表
    this.getRoleList()
  },
  components: {
    prccesspanel: () => import('./components/processpanel.vue')
  }
}
</script>

<style lang="scss">
.employees {
  margin: 20px;
  .mycard {
    margin-top: 20px;
  }
  .el-table__header {
    th:last-child {
      .cell {
        text-align: center;
      }
    }
  }
  .avatar {
    width: 30px;
    height: 30px;
  }
  .myitem {
    text-align: center;
  }
}
</style>
